home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / sbhc.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  7.6 KB  |  287 lines

  1. /* Copyright (C) 1994, 1995, 1996, 1998 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: sbhc.c,v 1.2.2.1 2000/11/02 15:05:02 igorm Exp $ */
  20. /* Bounded Huffman code filters */
  21. #include "memory_.h"
  22. #include "stdio_.h"
  23. #include "gdebug.h"
  24. #include "strimpl.h"
  25. #include "sbhc.h"
  26. #include "shcgen.h"
  27.  
  28. /* ------ BoundedHuffmanEncode ------ */
  29.  
  30. private_st_BHCE_state();
  31.  
  32. /* Initialize BoundedHuffmanEncode filter. */
  33. private int
  34. s_BHCE_reinit(stream_state * st)
  35. {
  36.     stream_BHCE_state *const ss = (stream_BHCE_state *) st;
  37.  
  38.     ss->encode.count = ss->definition.num_values;
  39.     s_bhce_init_inline(ss);
  40.     return 0;
  41. }
  42. private int
  43. s_BHCE_init(register stream_state * st)
  44. {
  45.     stream_BHCE_state *const ss = (stream_BHCE_state *) st;
  46.     hce_code *encode = ss->encode.codes =
  47.     (hce_code *) gs_alloc_byte_array(st->memory,
  48.                      ss->definition.num_values,
  49.                      sizeof(hce_code), "BHCE encode");
  50.  
  51.     if (encode == 0)
  52.     return ERRC;
  53. /****** WRONG ******/
  54.     hc_make_encoding(encode, &ss->definition);
  55.     return s_BHCE_reinit(st);
  56. }
  57.  
  58. /* Release the filter. */
  59. private void
  60. s_BHCE_release(stream_state * st)
  61. {
  62.     stream_BHCE_state *const ss = (stream_BHCE_state *) st;
  63.  
  64.     gs_free_object(st->memory, ss->encode.codes, "BHCE encode");
  65. }
  66.  
  67. /* Process a buffer. */
  68. private int
  69. s_BHCE_process(stream_state * st, stream_cursor_read * pr,
  70.            stream_cursor_write * pw, bool last)
  71. {
  72.     stream_BHCE_state *const ss = (stream_BHCE_state *) st;
  73.     const byte *p = pr->ptr;
  74.     const byte *rlimit = pr->limit;
  75.     byte *q = pw->ptr;
  76.     byte *wlimit = pw->limit - (hc_bits_size >> 3);
  77.     const hce_code *encode = ss->encode.codes;
  78.     uint num_values = ss->definition.num_values;
  79.     uint zero_runs = ss->EncodeZeroRuns;
  80.     uint zero_max = num_values - zero_runs + (ss->EndOfData ? 0 : 1);
  81.     uint zero_value = (zero_max > 1 ? 0 : 0x100);
  82.     int zeros = ss->zeros;
  83.     int status = 0;
  84.  
  85.     hce_declare_state;
  86.  
  87.     hce_load_state();
  88.     while (p < rlimit && q < wlimit) {
  89.     uint value = *++p;
  90.     const hce_code *cp;
  91.  
  92.     if (value >= num_values) {
  93.         status = ERRC;
  94.         break;
  95.     }
  96.     if (value == zero_value) {    /* Accumulate a run of zeros. */
  97.         ++zeros;
  98.         if (zeros != zero_max)
  99.         continue;
  100.         /* We've scanned the longest run we can encode. */
  101.         cp = &encode[zeros - 2 + zero_runs];
  102.         zeros = 0;
  103.         hc_put_code((stream_hc_state *) ss, q, cp);
  104.         continue;
  105.     }
  106.     /* Check whether we need to put out a zero run. */
  107.     if (zeros > 0) {
  108.         --p;
  109.         cp = (zeros == 1 ? &encode[0] :
  110.           &encode[zeros - 2 + zero_runs]);
  111.         zeros = 0;
  112.         hc_put_code((stream_hc_state *) ss, q, cp);
  113.         continue;
  114.     }
  115.     cp = &encode[value];
  116.     hc_put_code((stream_hc_state *) ss, q, cp);
  117.     }
  118.     if (q >= wlimit)
  119.     status = 1;
  120.     wlimit = pw->limit;
  121.     if (last && status == 0) {
  122.     if (zeros > 0) {    /* Put out a final run of zeros. */
  123.         const hce_code *cp = (zeros == 1 ? &encode[0] :
  124.                   &encode[zeros - 2 + zero_runs]);
  125.  
  126.         if (!hce_bits_available(cp->code_length))
  127.         status = 1;
  128.         else {
  129.         hc_put_code((stream_hc_state *) ss, q, cp);
  130.         zeros = 0;
  131.         }
  132.     }
  133.     if (ss->EndOfData) {    /* Put out the EOD code if we have room. */
  134.         const hce_code *cp = &encode[num_values - 1];
  135.  
  136.         if (!hce_bits_available(cp->code_length))
  137.         status = 1;
  138.         else
  139.         hc_put_code((stream_hc_state *) ss, q, cp);
  140.     } else {
  141.         if (q >= wlimit)
  142.         status = 1;
  143.     }
  144.     if (!status) {
  145.         q = hc_put_last_bits((stream_hc_state *) ss, q);
  146.         goto ns;
  147.     }
  148.     }
  149.     hce_store_state();
  150.   ns:pr->ptr = p;
  151.     pw->ptr = q;
  152.     ss->zeros = zeros;
  153.     return (p == rlimit ? 0 : 1);
  154. }
  155.  
  156. /* Stream template */
  157. const stream_template s_BHCE_template =
  158. {&st_BHCE_state, s_BHCE_init, s_BHCE_process,
  159.  1, hc_bits_size >> 3, s_BHCE_release, NULL, s_BHCE_reinit
  160. };
  161.  
  162. /* ------ BoundedHuffmanDecode ------ */
  163.  
  164. private_st_BHCD_state();
  165.  
  166. #define hcd_initial_bits 7    /* arbitrary, >= 1 and <= 8 */
  167.  
  168. /* Initialize BoundedHuffmanDecode filter. */
  169. private int
  170. s_BHCD_reinit(stream_state * st)
  171. {
  172.     stream_BHCD_state *const ss = (stream_BHCD_state *) st;
  173.  
  174.     ss->decode.count = ss->definition.num_values;
  175.     s_bhcd_init_inline(ss);
  176.     return 0;
  177. }
  178. private int
  179. s_BHCD_init(register stream_state * st)
  180. {
  181.     stream_BHCD_state *const ss = (stream_BHCD_state *) st;
  182.     uint initial_bits = ss->decode.initial_bits =
  183.     min(hcd_initial_bits, ss->definition.num_counts);
  184.     uint dsize = hc_sizeof_decoding(&ss->definition, initial_bits);
  185.     hcd_code *decode = ss->decode.codes =
  186.     (hcd_code *) gs_alloc_byte_array(st->memory, dsize,
  187.                      sizeof(hcd_code), "BHCD decode");
  188.  
  189.     if (decode == 0)
  190.     return ERRC;
  191. /****** WRONG ******/
  192.     hc_make_decoding(decode, &ss->definition, initial_bits);
  193.     st->min_left = 1;
  194.     return s_BHCD_reinit(st);
  195. }
  196.  
  197. /* Release the filter. */
  198. private void
  199. s_BHCD_release(stream_state * st)
  200. {
  201.     stream_BHCD_state *const ss = (stream_BHCD_state *) st;
  202.  
  203.     gs_free_object(st->memory, ss->decode.codes, "BHCD decode");
  204. }
  205.  
  206. /* Process a buffer. */
  207. private int
  208. s_BHCD_process(stream_state * st, stream_cursor_read * pr,
  209.            stream_cursor_write * pw, bool last)
  210. {
  211.     stream_BHCD_state *const ss = (stream_BHCD_state *) st;
  212.  
  213.     bhcd_declare_state;
  214.     byte *q = pw->ptr;
  215.     byte *wlimit = pw->limit;
  216.     const hcd_code *decode = ss->decode.codes;
  217.     uint initial_bits = ss->decode.initial_bits;
  218.     uint zero_runs = ss->EncodeZeroRuns;
  219.     int status = 0;
  220.     int eod = (ss->EndOfData ? ss->definition.num_values - 1 : -1);
  221.  
  222.     bhcd_load_state();
  223.   z:for (; zeros > 0; --zeros) {
  224.     if (q >= wlimit) {
  225.         status = 1;
  226.         goto out;
  227.     }
  228.     *++q = 0;
  229.     }
  230.     for (;;) {
  231.     const hcd_code *cp;
  232.     int clen;
  233.  
  234.     hcd_ensure_bits(initial_bits, x1);
  235.     cp = &decode[hcd_peek_var_bits(initial_bits)];
  236.       w1:if (q >= wlimit) {
  237.         status = 1;
  238.         break;
  239.     }
  240.     if ((clen = cp->code_length) > initial_bits) {
  241.         if (!hcd_bits_available(clen)) {    /* We don't have enough bits for */
  242.         /* all possible codes that begin this way, */
  243.         /* but we might have enough for */
  244.         /* the next code. */
  245. /****** NOT IMPLEMENTED YET ******/
  246.         break;
  247.         }
  248.         clen -= initial_bits;
  249.         hcd_skip_bits(initial_bits);
  250.         hcd_ensure_bits(clen, out);        /* can't exit */
  251.         cp = &decode[cp->value + hcd_peek_var_bits(clen)];
  252.         hcd_skip_bits(cp->code_length);
  253.     } else {
  254.         hcd_skip_bits(clen);
  255.     }
  256.     if (cp->value >= zero_runs) {
  257.         if (cp->value == eod) {
  258.         status = EOFC;
  259.         goto out;
  260.         }
  261.         /* This code represents a run of zeros, */
  262.         /* not a single output value. */
  263.         zeros = cp->value - zero_runs + 2;
  264.         goto z;
  265.     }
  266.     *++q = cp->value;
  267.     continue;
  268.     /* We don't have enough bits for all possible */
  269.     /* codes, but we might have enough for */
  270.     /* the next code. */
  271.       x1:cp = &decode[(bits & ((1 << bits_left) - 1)) <<
  272.              (initial_bits - bits_left)];
  273.     if ((clen = cp->code_length) <= bits_left)
  274.         goto w1;
  275.     break;
  276.     }
  277.   out:bhcd_store_state();
  278.     pw->ptr = q;
  279.     return status;
  280. }
  281.  
  282. /* Stream template */
  283. const stream_template s_BHCD_template =
  284. {&st_BHCD_state, s_BHCD_init, s_BHCD_process, 1, 1, s_BHCD_release,
  285.  NULL, s_BHCD_reinit
  286. };
  287.